package com.jss.lanouhn.fristapp.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.adapters.HomeBigAdapter;
import com.jss.lanouhn.fristapp.modles.homeModles.HomeRecyclerView;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CategoryRealLastActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private TextView textView;
    private ImageView iv_back,iv_select;
    private HomeBigAdapter adapter;
    private HomeRecyclerView homeRecyclerView;
    private String url,lastName;
    private int lastId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_real_last);
        iv_select= (ImageView) findViewById(R.id.iv_category_real_last);
        iv_back= (ImageView) findViewById(R.id.btn_back_credit4);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        textView= (TextView) findViewById(R.id.tv_category_real_last);
        recyclerView= (RecyclerView) findViewById(R.id.rv_category_real_last);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        Bundle bundle=getIntent().getExtras();
        lastId=bundle.getInt("lastId");
        lastName=bundle.getString("lastName");
        textView.setText(lastName);
        initData();
    }

    private void initData() {
        url="http://api.liwushuo.com/v2/channels/"+lastId+"/items_v2?order_by=now&limit=20&offset=0";
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Toast.makeText(CategoryRealLastActivity.this, "没有请求到数据", Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        homeRecyclerView = gson.fromJson(result, HomeRecyclerView.class);
                        if (homeRecyclerView.getData().getItems().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            Toast.makeText(CategoryRealLastActivity.this, "网络解析数据错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }
        }).start();
    }
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                adapter=new HomeBigAdapter(CategoryRealLastActivity.this,homeRecyclerView.getData().getItems());
                recyclerView.setAdapter(adapter);
            }
        }
    };
}
